package FEMSolver;


import MathLib.triangulation.SaveStructures.Node;
import syntaxanalyser.PostfiksXY;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author mark_o
 */
public class FEMElementMatrixGenerator {

    //i  a
    //j  b
    //m  c
    public static double kx;
    public static double ky;

    public static double Pex;
    public static double Pey;

    public static double[] GenerateF(Node A,Node B,Node C, String F){
        double Fa;
        double Fb;
        double Fc;

        try {
            Fa = PostfiksXY.Calculate(F, A.x, A.y);
            Fb = PostfiksXY.Calculate(F, B.x, B.y);
            Fc = PostfiksXY.Calculate(F, C.x, C.y);

        } catch (Exception ex) {
            Fa = 0;
            Fb = 0;
            Fc = 0;
        }
        double[][] Me = GenerateMe(A, B, C);
        return new  double[]{
            Me[0][0]*Fa+Me[0][1]*Fb+Me[0][2]*Fc,
            Me[1][0]*Fa+Me[1][1]*Fb+Me[1][2]*Fc,
            Me[2][0]*Fa+Me[2][1]*Fb+Me[2][2]*Fc
        };
    }


    public static double[][] GenerateKe(Node A,Node B,Node C){
        double d = 1.0/(4.0*ComputeTriangleArea(A, B, C)); //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! чи *2 чи *4
        /*double[][] res = new double[][]{
        { d*((C.x - B.x)*(C.x - B.x)*ky + (C.y - B.y)*(C.y - B.y)*kx),  d*((B.y -C.y)*(C.y - A.y)*kx + (B.x -C.x)*(C.x - A.x)*ky), d*((B.y -C.y)*(A.y - B.y)*kx +  (B.x -C.x)*(A.x - B.x)*ky) },

        { d*((B.y -C.y)*(C.y - A.y)*kx + (B.x -C.x)*(C.x - A.x)*ky),  d*((C.y - A.y)*(C.y - A.y)*kx + (C.x - A.x)*(C.x - A.x)*ky), d*((C.y -A.y)*(A.y - B.y)*kx +  (C.x -A.x)*(A.x - B.x)*ky) },

        { d*((B.y -C.y)*(A.y - B.y)*kx + (B.x -C.x)*(A.x - B.x)*ky),  d*((C.y - A.y)*(A.y - B.y)*kx + (C.x -A.x)*(A.x - B.x)*ky), d*((A.y - B.y)*(A.y - B.y)*kx +  (A.x - B.x)*(A.x - B.x)*ky) }

        };*/

        double bi = B.y-C.y,
               bj = C.y-A.y,
               bk = A.y-B.y;
        double ci = C.x-B.x,
               cj = A.x-C.x,
               ck = B.x-A.x;
        return new double[][]{
            {d*bi*bi*kx + d*ci*ci*ky, d*bi*bj*kx+ d*ci*cj*ky, d*bi*bk*kx + d*ci*ck*ky},
            {d*bi*bj*kx + d*ci*cj*ky, d*bj*bj*kx+ d*cj*cj*ky, d*bj*bk*kx + d*cj*ck*ky},
            {d*bi*bk*kx + d*ci*ck*ky, d*bk*bj*kx+ d*ck*cj*ky, d*bk*bk*kx + d*ck*ck*ky}
        };
    }

    public static double[][] GeneratePe(Node A,Node B,Node C){
        double d = 1.0/(6.0); 
        double bi = B.y-C.y,
               bj = C.y-A.y,
               bk = A.y-B.y;
        double ci = C.x-B.x,
               cj = A.x-C.x,
               ck = B.x-A.x;
        return new double[][]{
            {d*bi*Pex + d*ci*Pey, d*bj*Pex+ d*cj*Pey, d*bk*Pex + d*ck*Pey},
            {d*bi*Pex + d*ci*Pey, d*bj*Pex+ d*cj*Pey, d*bk*Pex + d*ck*Pey},
            {d*bi*Pex + d*ci*Pey, d*bj*Pex+ d*cj*Pey, d*bk*Pex + d*ck*Pey}
        };
    }

    public static double[][] GenerateMe(Node A,Node B,Node C){
        double d = (ComputeTriangleArea(A, B, C))/12.0;
        return new double[][]{
            { 2*d, d, d },
            { d, 2*d, d },
            { d, d, 2*d }

        };
    }

    public static double ComputeTriangleArea(Node A,Node B,Node C){
        
        double a =Math.sqrt( (A.x - B.x)*(A.x - B.x) + (A.y - B.y)*(A.y - B.y) );   //a-b
        double b =Math.sqrt( (C.x - B.x)*(C.x - B.x) + (C.y - B.y)*(C.y - B.y) );
        double c =Math.sqrt( (A.x - C.x)*(A.x - C.x) + (A.y - C.y)*(A.y - C.y) );
        double p = (a+b+c)/2.0;
        return Math.sqrt( p*(p-a)*(p-b)*(p-c) );
    }
}
